package com.lms.logisticsmanagementsystem.service.Impl;

import com.lms.logisticsmanagementsystem.mapper.LoginMapper;
import com.lms.logisticsmanagementsystem.model.LoginManage;
import com.lms.logisticsmanagementsystem.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;



    @Override
     public boolean checkLogin(LoginManage login_manage) {
        LoginManage loginManageFromSQL = loginMapper.checkLogin(login_manage.getUsername());
        System.out.println("数据库中记录：" + loginManageFromSQL);

        return loginManageFromSQL != null
                && login_manage.getPassword().equals(loginManageFromSQL.getPassword())
                && login_manage.getUserIdentity().equals(loginManageFromSQL.getUserIdentity());
    }

    @Override
    public boolean checkRegisterInfo(LoginManage loginManage) {
        //查看数据库中是否存有当前注册信息记录
        Integer count = loginMapper.checkRegisterCount(loginManage.getUsername());
        System.out.println(count);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void writeUserInfo(LoginManage loginManage) {
        loginMapper.writeUserInfo(loginManage.getUsername(),loginManage.getPassword(),loginManage.getUserIdentity());
    }

    @Override
    public boolean checkOldPassword(String username, String oldPasswd) {
        String passwordFromDatabase = loginMapper.checkOldPassword(username);
//        System.out.println(passwordFromDatabase);
        if (oldPasswd.equals(passwordFromDatabase)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void updatePassword(String username, String newPasswd) {
        loginMapper.updatePassword(username,newPasswd);
    }
}
